<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container" style="margin: 10px 0 10px 0">
      <el-select
        v-model="dictUid"
        placeholder="请选择字典名称"
        style="width: 150px"
      >
        <el-option
          v-for="item in dictTypeList"
          :key="item.uid"
          :label="item.dictName"
          :value="item.uid"
        ></el-option>
      </el-select>

      <el-input
        clearable
        @keyup.enter.native="handleFind"
        class="filter-item"
        style="width: 200px"
        v-model="itemLabelQuery"
        placeholder="请输入字典标签"
      ></el-input>

      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFind"
        >查找</el-button
      >
      <el-button
        class="filter-item"
        type="primary"
        @click="handleAdd"
        icon="el-icon-edit"
        >添加</el-button
      >
      <el-button
        class="filter-item"
        type="danger"
        @click="handleDeleteBatch"
        icon="el-icon-delete"
        >删除选中</el-button
      >
    </div>

    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"></el-table-column>

      <el-table-column label="序号" width="60" align="center">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>

      <el-table-column label="字典项标签" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.itemLabel }}</span>
        </template>
      </el-table-column>

      <el-table-column label="字典项键值" width="100" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.$index % 5 == 0" type="warning">{{
            scope.row.itemValue
          }}</el-tag>
          <el-tag v-if="scope.$index % 5 == 1" type="success">{{
            scope.row.itemValue
          }}</el-tag>
          <el-tag v-if="scope.$index % 5 == 2" type="info">{{
            scope.row.itemValue
          }}</el-tag>
          <el-tag v-if="scope.$index % 5 == 3" type="danger">{{
            scope.row.itemValue
          }}</el-tag>
          <el-tag v-if="scope.$index % 5 == 4">{{
            scope.row.itemValue
          }}</el-tag>
        </template>
      </el-table-column>

      <!-- <el-table-column label="字典类型" width="200" align="center">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.sysDictType.dictType}}</el-tag>
        </template>
      </el-table-column> -->

      <el-table-column label="排序" width="50" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.sort }}</span>
        </template>
      </el-table-column>

      <el-table-column label="备注" width="200" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.remark }}</span>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" width="160" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" width="100" align="center">
        <template slot-scope="scope">
          <template v-if="scope.row.status == 1">
            <span>正常</span>
          </template>
          <template v-if="scope.row.status == 2">
            <span>推荐</span>
          </template>
          <template v-if="scope.row.status == 0">
            <span>已删除</span>
          </template>
        </template>
      </el-table-column>

      <el-table-column label="操作" fixed="right" min-width="240">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row)" type="primary" size="small"
            >编辑</el-button
          >
          <el-button @click="handleDelete(scope.row)" type="danger" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item
          label="字典项标签"
          :label-width="formLabelWidth"
          prop="itemLabel"
        >
          <el-input v-model="form.itemLabel" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item
          label="字典项键值"
          :label-width="formLabelWidth"
          prop="itemValue"
        >
          <el-input v-model="form.itemValue" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item
          label="字典类型"
          :label-width="formLabelWidth"
          prop="dictUid"
        >
          <el-select
            disabled
            v-model="form.dictUid"
            placeholder="请选择字典类型"
            style="width: 200px"
          >
            <el-option
              v-for="item in dictTypeList"
              :key="item.uid"
              :label="item.dictName"
              :value="item.uid"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="排序" :label-width="formLabelWidth" prop="sort">
          <el-input v-model="form.sort" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="form.remark" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getSysDictItemPageList,
  addSysDictItem,
  updateSysDictItem,
  deleteSysDictItem,
  getSysDictItem,
} from "@/api/system/sysdictitem";

import { getSysDictPageList } from "@/api/system/sysdict";

export default {
  data() {
    return {
      dictUid: null, // 从SysDictType传递过来的
      dictTypeMap: {}, // DictType对象集合
      itemLabelQuery: "", // 字典标签查询
      multipleSelection: [], //多选，用于批量删除
      dictTypeList: [],
      tableData: [],
      keyword: "",
      currentPage: 1,
      pageSize: 10,
      total: 0, //总数量
      title: "增加字典数据",
      dialogFormVisible: false, //控制弹出框
      formLabelWidth: "120px",
      isEditForm: false,
      form: {},
      listClassType: [
        { key: 1, label: "default", value: "default" },
        { key: 2, label: "primary", value: "primary" },
        { key: 3, label: "success", value: "success" },
        { key: 4, label: "info", value: "info" },
        { key: 5, label: "warning", value: "warning" },
        { key: 6, label: "danger", value: "danger" },
      ],
      rules: {
        itemLabel: [
          { required: true, message: "字典标签不能为空", trigger: "blur" },
          { min: 1, max: 20, message: "长度在1到20个字符" },
        ],
        itemValue: [
          { required: true, message: "字典键值不能为空", trigger: "blur" },
          { min: 1, max: 20, message: "长度在1到20个字符" },
        ],
        dictUid: [
          { required: true, message: "字典类型不能为空", trigger: "blur" },
        ],
        isDefault: [
          { required: true, message: "系统默认不能为空", trigger: "blur" },
        ],
        isPublish: [
          { required: true, message: "发布状态不能为空", trigger: "blur" },
        ],
        sort: [
          { required: true, message: "排序字段不能为空", trigger: "blur" },
          { pattern: /^[0-9]\d*$/, message: "排序字段只能为自然数" },
        ],
      },
    };
  },
  created() {
    //传递过来的dictUid
    this.dictUid = this.$route.query.dictUid;
    this.SysDictItemPageList();
    this.SysDictPageList();
  },
  methods: {
    SysDictItemPageList: function () {
      var params = {};
      params.dictUid = this.dictUid;
      params.itemLabel = this.itemLabelQuery;
      params.currentPage = this.currentPage;
      params.pageSize = this.pageSize;
      getSysDictItemPageList(params).then((response) => {
        if (response.code == 200) {
          this.tableData = response.data.records;
          this.currentPage = response.data.current;
          this.pageSize = response.data.size;
          this.total = response.data.total;
        }
      });
    },
    SysDictPageList: function () {
      var params = {};
      params.keyword = "";
      params.currentPage = 0;
      params.pageSize = 100;
      getSysDictPageList(params).then((response) => {
        if (response.code == 200) {
          this.dictTypeList = response.data.records;
        }
      });
    },
    // 这里可以设置一些初始值
    getFormObject: function () {
      var formObject = {
        isPublish: "1",
        sort: 0,
        isDefault: 0,
        listClass: "",
      };
      return formObject;
    },
    handleFind: function () {
      this.sysDictDataList();
    },
    handleAdd: function () {
      this.title = "增加数据字典";
      this.dialogFormVisible = true;
      this.form = this.getFormObject();
      this.form.dictUid = this.dictUid;
      this.isEditForm = false;
    },
    handleEdit: function (row) {
      title: "编辑字典数据";
      this.dialogFormVisible = true;
      this.isEditForm = true;
      this.form = row;
      this.form.dictUid = this.dictUid;
    },
    handleDelete: function (row) {
      var that = this;
      this.$confirm("此操作将把字典数据删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let sysDictData = {};
          sysDictData.uid = row.uid;
          let deleteList = [];
          deleteList.push(sysDictData);
          deleteBatchSysDictData(deleteList).then((response) => {
            console.log(response);
            this.$message({
              type: "success",
              message: response.data,
            });
            that.sysDictDataList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleDeleteBatch: function (row) {
      var that = this;
      if (that.multipleSelection.length <= 0) {
        this.$message({
          type: "error",
          message: "请先选中需要删除的内容！",
        });
        return;
      }
      this.$confirm("此操作将把选中字典数据删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteBatchSysDictData(that.multipleSelection).then((response) => {
            this.$message({
              type: "success",
              message: response.data,
            });
            that.sysDictDataList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleCurrentChange: function (val) {
      this.currentPage = val;
      this.sysDictDataList();
    },
    // 改变多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    submitForm: function () {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          console.log("校验出错");
        } else {
          if (this.isEditForm) {
            updateSysDictItem(this.form).then((response) => {
              console.log(response);
              if (response.code == 200) {
                this.$message({
                  type: "success",
                  message: response.data,
                });
                this.dialogFormVisible = false;
                this.SysDictItemPageList();
              } else {
                this.$message({
                  type: "success",
                  message: response.data,
                });
              }
            });
          } else {
            addSysDictItem(this.form).then((response) => {
              if (response.code == 200) {
                this.$message({
                  type: "success",
                  message: response.data,
                });
                this.dialogFormVisible = false;
                this.SysDictItemPageList();
              } else {
                this.$message({
                  type: "error",
                  message: response.data,
                });
              }
            });
          }
        }
      });
    },
  },
};
</script>
